package com.monkey_pig.memoryalbum.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;

import com.monkey_pig.memoryalbum.R;
import com.monkey_pig.memoryalbum.utils.BaseActivity;
import com.monkey_pig.memoryalbum.view.SlidingTabLayout;

import java.util.ArrayList;

public class MainActivity extends BaseActivity {

    private SlidingTabLayout mSlidingTabLayout;
    private ViewPager mViewPager;
    private Toolbar toolbar;
    private ArrayList<Fragment> listFrament;
    private RemindAdapter myadapter;
    private AllPhotoFragment allPhotoFragment;
    private MyFootFragment myFootFragment;
    public FragmentManager fragmentManager;
    public String curFragmentTag = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        初始化actionbar
        initActionBar();
        //找到设置的viewpager，与slidingTablayout
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.tab_layout);
        //添加fragment碎片到一个ArrayList中去
        listFrament = new ArrayList<>();
         allPhotoFragment= new AllPhotoFragment();
        myFootFragment = new MyFootFragment();
        listFrament.add(allPhotoFragment);
        listFrament.add(myFootFragment);

        //设置配适器
        myadapter = new RemindAdapter(getSupportFragmentManager(), this);
        //将配适器添加到ViewPager中
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setAdapter(myadapter);

        // 定义 SlidingTabLayout，设置选中颜色、背景颜色
        mSlidingTabLayout.setSelectedIndicatorColors(getResources().getColor(R.color.greyblue));
        mSlidingTabLayout.setBackgroundColor(getResources().getColor(R.color.white));
        //设置自定义的布局（一个LinearLayou与一个TextView）
        mSlidingTabLayout.setCustomTabView(R.layout.view_tab, R.id.tabText);
        mSlidingTabLayout.setViewPager(mViewPager); // 加载ViewPager
    }
    //toolbar的配置代码
    public void initActionBar() {
        //设置标题
        setTitle("回忆册");
        //实例化布局组件
        toolbar = (Toolbar) findViewById(R.id.activity_main_toolbar);
        //添加logo
        toolbar.setLogo(R.drawable.account_ic_share_weixin);
        //添加配置到布局中
        setSupportActionBar(toolbar);
        //设置导航图标
//        toolbar.setNavigationIcon(R.drawable.abs__ic_ab_back_holo_light);
//        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                finish();
//            }
//        });
        //下面这行代码可有可无
//        toolbar.setOnCreateContextMenuListener(this);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return  true;
    }















    public class RemindAdapter extends FragmentStatePagerAdapter {

        private String[] titles;
        private Context ctx;

        public RemindAdapter(FragmentManager fm, Context ctx) {
            super(fm);
            this.ctx = ctx;
            //设置碎片tab的title，来自一个xml文件
            titles = ctx.getResources().getStringArray(R.array.remind_type);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {//全部订单
                return allPhotoFragment;
            } else {//未评价订单
                return myFootFragment;
            }
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public int getCount() {
            return titles.length;
        }
    }
}
